//
// Created by uts on 10/28/21.
//

#ifndef AVCPP_APPSHAREMEMORY_H
#define AVCPP_APPSHAREMEMORY_H
#include <iostream>

#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <unistd.h>
#include <cstring>
#include <errno.h>


#define OPEN_MEM_ERROR -1;
#define MAP_MEM_ERROR -2;
struct DetectResult{
    int left;
    int top;
    int right;
    int bottom;
    int probability;
    int category;
};

struct DataBlock{
    unsigned int flag = 1;
    unsigned long channel = 0l;
    int length = 0;
    int frame_serial_no = 0;
    int supplement_position = 0; //for python unpack
    unsigned long timestamp = 0;
    int width = 0;
    int height = 0;
    int detected_count = 0;
    int data_length = 0;
    unsigned char *data;
    DetectResult *results;
};

class AppShareMemory {
public:
    AppShareMemory(const char *name,int size,int block_size);     //Constructor
    ~AppShareMemory();                                   //deConstructor
    bool push(DataBlock *block);
    bool push(unsigned int flag,unsigned long channel,
              int length,int frame_serial_no,
              int supplement_position,unsigned long timestamp,
              int width,int height,int data_length,unsigned char *data);
    bool pull(DataBlock *block);
    void setCheck(bool check_overflow);
private:
    const char* name;                                  //the key of share memory
    int m_size = 0;                                 //the size of share memory block
    long block_size = 0;
    int share_memory_fd;
    unsigned char *mem_pointer;
    unsigned  char *initial_mem_pointer;
    long total_size = 0l;
    int error_number = 0;
    int cursor = 0;
    bool check_overflow = true;
};


#endif //AVCPP_APPSHAREMEMORY_H